package com.kingdee.eas.custom;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import oracle.jdbc.OracleDriver;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.dao.query.SQLExecutorFactory;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.base.permission.UserFactory;
import com.kingdee.eas.base.permission.UserInfo;
import com.kingdee.eas.basedata.assistant.AccountBankCollection;
import com.kingdee.eas.basedata.assistant.AccountBankFactory;
import com.kingdee.eas.basedata.assistant.CityCollection;
import com.kingdee.eas.basedata.assistant.CityFactory;
import com.kingdee.eas.basedata.assistant.CountryCollection;
import com.kingdee.eas.basedata.assistant.CountryFactory;
import com.kingdee.eas.basedata.assistant.CurrencyCollection;
import com.kingdee.eas.basedata.assistant.CurrencyFactory;
import com.kingdee.eas.basedata.assistant.IAccountBank;
import com.kingdee.eas.basedata.assistant.ICity;
import com.kingdee.eas.basedata.assistant.ICountry;
import com.kingdee.eas.basedata.assistant.ICurrency;
import com.kingdee.eas.basedata.assistant.IProject;
import com.kingdee.eas.basedata.assistant.IProvince;
import com.kingdee.eas.basedata.assistant.ISettlementType;
import com.kingdee.eas.basedata.assistant.IVoucherAbstract;
import com.kingdee.eas.basedata.assistant.ProjectCollection;
import com.kingdee.eas.basedata.assistant.ProjectFactory;
import com.kingdee.eas.basedata.assistant.ProvinceCollection;
import com.kingdee.eas.basedata.assistant.ProvinceFactory;
import com.kingdee.eas.basedata.assistant.SettlementTypeCollection;
import com.kingdee.eas.basedata.assistant.SettlementTypeFactory;
import com.kingdee.eas.basedata.assistant.VoucherAbstractCollection;
import com.kingdee.eas.basedata.assistant.VoucherAbstractFactory;
import com.kingdee.eas.basedata.master.account.AccountViewCollection;
import com.kingdee.eas.basedata.master.account.AccountViewFactory;
import com.kingdee.eas.basedata.master.account.IAccountView;
import com.kingdee.eas.basedata.master.auxacct.AsstActTypeCollection;
import com.kingdee.eas.basedata.master.auxacct.AsstActTypeFactory;
import com.kingdee.eas.basedata.master.auxacct.AsstActTypeInfo;
import com.kingdee.eas.basedata.master.auxacct.IAsstActType;
import com.kingdee.eas.basedata.master.cssp.CSSPGroupCollection;
import com.kingdee.eas.basedata.master.cssp.CSSPGroupFactory;
import com.kingdee.eas.basedata.master.cssp.CSSPGroupInfo;
import com.kingdee.eas.basedata.master.cssp.CSSPGroupStandardCollection;
import com.kingdee.eas.basedata.master.cssp.CSSPGroupStandardFactory;
import com.kingdee.eas.basedata.master.cssp.CSSPGroupStandardInfo;
import com.kingdee.eas.basedata.master.cssp.CustomerCollection;
import com.kingdee.eas.basedata.master.cssp.CustomerCompanyBankCollection;
import com.kingdee.eas.basedata.master.cssp.CustomerCompanyBankFactory;
import com.kingdee.eas.basedata.master.cssp.CustomerCompanyInfoCollection;
import com.kingdee.eas.basedata.master.cssp.CustomerCompanyInfoFactory;
import com.kingdee.eas.basedata.master.cssp.CustomerCompanyInfoInfo;
import com.kingdee.eas.basedata.master.cssp.CustomerFactory;
import com.kingdee.eas.basedata.master.cssp.CustomerInfo;
import com.kingdee.eas.basedata.master.cssp.ICustomer;
import com.kingdee.eas.basedata.master.cssp.ICustomerCompanyBank;
import com.kingdee.eas.basedata.master.cssp.ICustomerCompanyInfo;
import com.kingdee.eas.basedata.master.cssp.ISupplierCompanyBank;
import com.kingdee.eas.basedata.master.cssp.ISupplierCompanyInfo;
import com.kingdee.eas.basedata.master.cssp.SupplierCompanyBankCollection;
import com.kingdee.eas.basedata.master.cssp.SupplierCompanyBankFactory;
import com.kingdee.eas.basedata.master.cssp.SupplierCompanyInfoCollection;
import com.kingdee.eas.basedata.master.cssp.SupplierCompanyInfoFactory;
import com.kingdee.eas.basedata.master.cssp.UsedStatusEnum;
import com.kingdee.eas.basedata.org.AdminOrgUnitCollection;
import com.kingdee.eas.basedata.org.AdminOrgUnitFactory;
import com.kingdee.eas.basedata.org.CompanyOrgUnitCollection;
import com.kingdee.eas.basedata.org.CompanyOrgUnitFactory;
import com.kingdee.eas.basedata.org.CompanyOrgUnitInfo;
import com.kingdee.eas.basedata.org.CostCenterOrgUnitCollection;
import com.kingdee.eas.basedata.org.CostCenterOrgUnitFactory;
import com.kingdee.eas.basedata.org.CtrlUnitFactory;
import com.kingdee.eas.basedata.org.CtrlUnitInfo;
import com.kingdee.eas.basedata.org.IAdminOrgUnit;
import com.kingdee.eas.basedata.org.ICompanyOrgUnit;
import com.kingdee.eas.basedata.org.ICostCenterOrgUnit;
import com.kingdee.eas.basedata.person.IPerson;
import com.kingdee.eas.basedata.person.PersonCollection;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.cp.bc.ExpenseTypeCollection;
import com.kingdee.eas.cp.bc.ExpenseTypeFactory;
import com.kingdee.eas.cp.bc.IExpenseType;
import com.kingdee.eas.fi.cas.AssItemsForCashPayInfo;
import com.kingdee.eas.fi.cas.AssItemsForCashRecInfo;
import com.kingdee.eas.fi.cas.FeeTypeCollection;
import com.kingdee.eas.fi.cas.FeeTypeFactory;
import com.kingdee.eas.fi.cas.FundFlowItemCollection;
import com.kingdee.eas.fi.cas.FundFlowItemFactory;
import com.kingdee.eas.fi.cas.IFeeType;
import com.kingdee.eas.fi.cas.IFundFlowItem;
import com.kingdee.eas.fi.cas.IPaymentBill;
import com.kingdee.eas.fi.cas.IPaymentBillType;
import com.kingdee.eas.fi.cas.IReceivingBill;
import com.kingdee.eas.fi.cas.IReceivingBillType;
import com.kingdee.eas.fi.cas.PaymentBillEntryInfo;
import com.kingdee.eas.fi.cas.PaymentBillFactory;
import com.kingdee.eas.fi.cas.PaymentBillInfo;
import com.kingdee.eas.fi.cas.PaymentBillTypeCollection;
import com.kingdee.eas.fi.cas.PaymentBillTypeFactory;
import com.kingdee.eas.fi.cas.ReceivingBillEntryInfo;
import com.kingdee.eas.fi.cas.ReceivingBillFactory;
import com.kingdee.eas.fi.cas.ReceivingBillInfo;
import com.kingdee.eas.fi.cas.ReceivingBillTypeCollection;
import com.kingdee.eas.fi.cas.ReceivingBillTypeFactory;
import com.kingdee.eas.fm.common.FMIsqlFacadeFactory;
import com.kingdee.eas.fm.common.IFMIsqlFacade;
import com.kingdee.eas.fm.fs.ISettBizType;
import com.kingdee.eas.fm.fs.SettBizTypeCollection;
import com.kingdee.eas.fm.fs.SettBizTypeFactory;
import com.kingdee.eas.framework.EffectedStatusEnum;
import com.kingdee.jdbc.rowset.IRowSet;




public class synchronizationDataFacadeControllerBean extends AbstractsynchronizationDataFacadeControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.custom.synchronizationDataFacadeControllerBean");
    
    
    
    Connection connect = null;
    Statement statement = null;
    ResultSet resultSet = null;
    
    public void siteCustomer() throws SQLException   {
    	Driver driver = new OracleDriver();
    	DriverManager.deregisterDriver(driver);
        connect = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "zr_hr", "oracle");
    	
        Properties pro = new Properties();
        pro.put("user", "zr_hr");
        pro.put("password", "oracle");
        connect = driver.connect("jdbc:oracle:thin:@localhost:1521:orcl", pro);

       
	}
    
    //管理单元仁济本院
	private CtrlUnitInfo ctrlUnitInfo=null;

	private UserInfo userInfo=null;
	
	private String orgNumber="jy";
	private String companyNumber="02";
	private String userNumber="user";

	private CompanyOrgUnitInfo companyOrgUnitInfo =null; 
    @Override
    protected void _syncCustomer(Context ctx) throws BOSException {
    	super._syncCustomer(ctx);
    	
    	
    	try {
			siteCustomer();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
    	
    	try {
    		companyOrgUnitInfo=CompanyOrgUnitFactory.getLocalInstance(ctx).getCompanyOrgUnitInfo("select id,number,name where number='"+companyNumber+"'");
    		
			ctrlUnitInfo=CtrlUnitFactory.getLocalInstance(ctx).getCtrlUnitInfo("select id,number,name where number='"+orgNumber+"'");
			userInfo=UserFactory.getLocalInstance(ctx).getUserInfo("select id ,number ,name where number='"+userNumber+"'");

		} catch (EASBizException e1) {
			e1.printStackTrace();
		}
		
    	
    	Map<String, CSSPGroupInfo> map=new HashMap<String, CSSPGroupInfo>();
		CSSPGroupCollection cSSPGroupCollection = CSSPGroupFactory.getLocalInstance(ctx).getCSSPGroupCollection();
		for(int i=0;i<cSSPGroupCollection.size();i++){
			CSSPGroupInfo info=cSSPGroupCollection.get(i);
			map.put(info.getNumber(), info);
		}
		
		
		IFMIsqlFacade iFMIsqlFacade = FMIsqlFacadeFactory.getLocalInstance(ctx);
		CSSPGroupStandardInfo cSSPGroupStandardInfo=new CSSPGroupStandardInfo();
		CSSPGroupStandardCollection cSSPGroupStandardCollection = CSSPGroupStandardFactory.getLocalInstance(ctx).getCSSPGroupStandardCollection("where type=1 and ISBASIC=1");
		if(cSSPGroupStandardCollection.size()>0){
			cSSPGroupStandardInfo=cSSPGroupStandardCollection.get(0);
		}

    	 ICustomer iCustomer = CustomerFactory.getLocalInstance(ctx);
    	 ICustomerCompanyInfo iCustomerCompanyInfo = CustomerCompanyInfoFactory.getLocalInstance(ctx);
    	
         try { 
             statement  = connect.createStatement();
             
        	 resultSet = statement.executeQuery("select a.khname as khname,a.khnumber as khnumber from T_BD_Customer a where a.tbzt='s' and a.clzt<>'s'" );          	        	 
        	 
        	 while (resultSet.next()){
        		 String error =null; 
        		 String number= resultSet.getString("khnumber"); 
        		 String name= resultSet.getString("khname");   
        	   try { 
	
            	 CustomerCollection  collection = iCustomer.getCustomerCollection("where number='"+number+"'");
            	 if(collection.size()>0){
            		 CustomerInfo info = collection.get(0);
            		 info.setName(name);
            		        		 
            	     iCustomer.update(new ObjectUuidPK(info.getId()), info);

            		 
            	 }else {
            		 CustomerInfo info = new CustomerInfo();
            		 CustomerCompanyInfoInfo iCustomerCompany = new CustomerCompanyInfoInfo();
    					
    				 String key="001"; 
    				 
    				 if(map.containsKey(key)){
    					    					      					
    					 if(null == name){
	    					throw  new RuntimeException("名称为空");
    					 }else {
     						info.setName(name);
 						}
    						 
    					if(null == number){
    						throw  new RuntimeException("编码为空");   	
//    						error += ";编码为空";
    					}else {
    						info.setNumber(number);
						}
    					   					
    					info.setId(BOSUuid.create(info.getBOSType().toString()));
    					
    					info.setCreator(userInfo);
    					info.setCU(ctrlUnitInfo);							
    					info.setUsedStatus(UsedStatusEnum.APPROVED);
    					info.setEffectedStatus(EffectedStatusEnum.EFFECTED);
    			
    				    //客户财务
    				    iCustomerCompany.setCompanyOrgUnit(companyOrgUnitInfo);
    				    iCustomerCompany.setEffectedStatus(EffectedStatusEnum.EFFECTED);
    				    iCustomerCompany.setCreator(userInfo);
    				    iCustomerCompany.setCU(ctrlUnitInfo);
    				    iCustomerCompany.setCustomer(info);
    				    
    				    iCustomerCompanyInfo.addnew(iCustomerCompany);
    					
    					info.setBrowseGroup(map.get(key));
    					iCustomer.addnew(info);
    					
    					String sql="insert into T_BD_CustomerGroupDetail (fid,FCustomerGroupStandardID,FCustomerGroupID,FCustomerID,FCustomerGroupFullName) " +
    							"values (?,?,?,?,?);";
                        Object[] paramArrayOfObject=new Object[]{BOSUuid.create("F3860252").toString(),cSSPGroupStandardInfo.getId().toString(),map.get(key).getId().toString(),info.getId().toString(),map.get(key).getName()};
    					iFMIsqlFacade.executeSql(sql, paramArrayOfObject);
    				 }
    			}
        	 Date date = new Date(); 
        	 Statement create = connect.createStatement();
        	      		   		 
        	create.executeUpdate(" update T_BD_Customer set clsj='"+date+"',clzt='s',clclie='s' where khnumber='"+number+"'" ); 
        	 
        	 
    	   }catch (Exception e) {
    		   error=e.getMessage();
    	   }finally{
    		   Date date = new Date(); 
          	   Statement create = connect.createStatement();
          	 if(error!=null){  
    		   create.executeUpdate(" update T_BD_Customer set clsj='"+date+"',clbz='"+ error +"',clzt='s',clclie='f' where khnumber='"+number+"'" ); 
          	 }
    	   }
            	
           }

         } catch (Exception e) {
             e.printStackTrace();
         }finally {
        	 
        	
             //第六步：关闭资源
                 try {
                     if (resultSet!=null) resultSet.close();
                     if (statement!=null) statement.close();
                     if (connect!=null) connect.close();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }
         }
         
    }
    
    @Override
    protected void _sycnReceivingBill(Context ctx) throws BOSException {
        try {				 
           siteCustomer();
	       PreparedStatement preState = connect.prepareStatement("select  * from T_EAS_PaymentBill where tbzt='s' and clzt<>'s'");
           ResultSet rs = preState.executeQuery();
           
           ICompanyOrgUnit iCompanyOrgUnit = CompanyOrgUnitFactory.getLocalInstance(ctx);
           IFeeType iFeeType = FeeTypeFactory.getLocalInstance(ctx);
           IPaymentBillType iPaymentBillType = PaymentBillTypeFactory.getLocalInstance(ctx);
           ICurrency iCurrency = CurrencyFactory.getLocalInstance(ctx);
           IAccountBank iAccountBank = AccountBankFactory.getLocalInstance(ctx);
           IAccountView iAccountView = AccountViewFactory.getLocalInstance(ctx);
           ISettBizType iSettBizType = SettBizTypeFactory.getLocalInstance(ctx);
           ISettlementType iSettlementType = SettlementTypeFactory.getLocalInstance(ctx);
           IFundFlowItem iFundFlowItem = FundFlowItemFactory.getLocalInstance(ctx);
           ICostCenterOrgUnit iCostCenterOrgUnit = CostCenterOrgUnitFactory.getLocalInstance(ctx);
           IVoucherAbstract iVoucherAbstract = VoucherAbstractFactory.getLocalInstance(ctx);
           IAdminOrgUnit iAdminOrgUnit = AdminOrgUnitFactory.getLocalInstance(ctx);
           IPerson iPerson = PersonFactory.getLocalInstance(ctx);
           IAsstActType iAsstActType = AsstActTypeFactory.getLocalInstance(ctx);
           ICustomerCompanyInfo iCustomerCompanyInfo = CustomerCompanyInfoFactory.getLocalInstance(ctx);
           ICustomerCompanyBank iCustomerCompanyBank = CustomerCompanyBankFactory.getLocalInstance(ctx);
           ISupplierCompanyBank iSupplierCompanyBank = SupplierCompanyBankFactory.getLocalInstance(ctx);
           ISupplierCompanyInfo iSupplierCompanyInfo = SupplierCompanyInfoFactory.getLocalInstance(ctx);
           IProject iProject = ProjectFactory.getLocalInstance(ctx);
           ICountry iCountry = CountryFactory.getLocalInstance(ctx);
           IProvince iProvince = ProvinceFactory.getLocalInstance(ctx);
           ICity iCity = CityFactory.getLocalInstance(ctx);
           IExpenseType iExpenseType = ExpenseTypeFactory.getLocalInstance(ctx);
           IPaymentBill iPaymentBill = PaymentBillFactory.getLocalInstance(ctx);
           
           while(rs.next()){
        	   String msg="";
        	   try{
	        	   PaymentBillInfo info=new PaymentBillInfo();
	        	   CompanyOrgUnitCollection gsNumCollection = iCompanyOrgUnit.getCompanyOrgUnitCollection("where number='"+rs.getString("gsNum")+"'");
	        	   if(gsNumCollection.size()==0){
	        		   throw new RuntimeException("找不到公司编码对应公司数据");
	        	   }
	        	   info.setCompany(gsNumCollection.get(0));
	        	   
	        	   if(null!=rs.getString("dlgsNum")&&!"".equals(rs.getString("dlgsNum"))){
	        		   CompanyOrgUnitCollection dlgsNumCollection = iCompanyOrgUnit.getCompanyOrgUnitCollection("where number='"+rs.getString("dlgsNum")+"'");
	            	   if(dlgsNumCollection.size()==0){
	            		   throw new RuntimeException("找不到代理付款公司编码对应的公司数据");
	            	   }
	            	   info.setAgentPayCompany(dlgsNumCollection.get(0));
	        	   }
	        	   
	        	   if(null==rs.getDate("ywrq")||"".equals(rs.getString("ywrq"))){
	        		   throw new RuntimeException("业务日期不能为空");
	        	   }
	        	   info.setBizDate(rs.getDate("ywrq"));
	        	   
	        	   if(null!=rs.getString("sflbNum")&&!"".equals(rs.getString("sflbNum"))){
	        		   FeeTypeCollection feeTypeCollection = iFeeType.getFeeTypeCollection("where number='"+rs.getString("sflbNum")+"'");
	        		   if(feeTypeCollection.size()==0){
	        			   throw new RuntimeException("找不到收付类别编码对应的收付类别数据");
	        		   }
	        		   info.setFeeType(feeTypeCollection.get(0));
	        	   }
	        	   
	        	   if(null==rs.getString("fklxNum")||"".equals(rs.getString("fklxNum"))){
	        		   throw new RuntimeException("付款类型编码不能为空");
	        	   }
	    		   PaymentBillTypeCollection paymentBillTypeCollection = iPaymentBillType.getPaymentBillTypeCollection("where number='"+rs.getString("fklxNum")+"'");
	    		   if(paymentBillTypeCollection.size()==0){
	    			   throw new RuntimeException("找不到付款类型编码对应的银行账号数据");
	    		   }
	    		   info.setPayBillType(paymentBillTypeCollection.get(0));
	        	   
	        	   
	        	   if(null==rs.getString("fkbb")||"".equals(rs.getString("fkbb"))){
	        		   throw new RuntimeException("付款币别编码不能为空");
	        	   }
	        		   
	        	   CurrencyCollection currencyCollection = iCurrency.getCurrencyCollection("where number='"+rs.getString("fkbb")+"'");
	    		   if(currencyCollection.size()==0){
	    			   throw new RuntimeException("找不到付款币别编码对应的币别数据");
	    		   }
	    		   info.setCurrency(currencyCollection.get(0));
	        	   
	        	   
	        	   if(null==rs.getBigDecimal("hl")||"".equals(rs.getString("hl"))){
	        		   throw new RuntimeException("汇率不能为空");
	        	   }
	        	   info.setExchangeRate(rs.getBigDecimal("hl"));
	        	   
	        	   if(null!=rs.getString("fkzhNum")&&!"".equals(rs.getString("fkzhNum"))){
	        		   AccountBankCollection accountBankCollection = iAccountBank.getAccountBankCollection("where number='"+rs.getString("fkzhNum")+"'");
	        		   if(accountBankCollection.size()==0){
	        			   throw new RuntimeException("找不到付款账户编码对应的银行账号数据");
	        		   }
	        		   info.setPayerAccountBank(accountBankCollection.get(0));
	        	   }
	        	   
	        	   if(null!=rs.getString("fkkmNum")&&!"".equals(rs.getString("fkkmNum"))){
	        		   AccountViewCollection accountViewCollection = iAccountView.getAccountViewCollection("where number='"+rs.getString("fkkmNum")+"'");
	        		   if(accountViewCollection.size()==0){
	        			   throw new RuntimeException("找不到付款科目编码对应的会计科目数据");
	        		   }
	        		   info.setPayerAccount(accountViewCollection.get(0));
	        	   }
	        	   
	        	   if(null!=rs.getString("ywzlNum")&&!"".equals(rs.getString("ywzlNum"))){
	        		   SettBizTypeCollection settBizTypeCollection = iSettBizType.getSettBizTypeCollection("where number='"+rs.getString("ywzlNum")+"'");
	        		   if(settBizTypeCollection.size()==0){
	        			   throw new RuntimeException("找不到业务种类编码对应的业务种类数据");
	        		   }
	        		   info.setBizType(settBizTypeCollection.get(0));
	        	   }
	        	   
	        	   if(null!=rs.getString("jsfsbm")&&!"".equals(rs.getString("jsfsbm"))){
	        		   EntityViewInfo view=new EntityViewInfo();
	        		   FilterInfo filter=new FilterInfo();
	        		   filter.getFilterItems().add(new FilterItemInfo("number",rs.getString("jsfsbm")));
	        		   view.setFilter(filter);
	        		   SettlementTypeCollection settlementTypeCollection = iSettlementType.getSettlementTypeCollection(view);
	        		   if(settlementTypeCollection.size()==0){
	        			   throw new RuntimeException("找不到结算方式编码对应的结算方式数据");
	        		   }
	        		   info.setSettlementType(settlementTypeCollection.get(0));
	        	   }
	        	   
	        	   if(null!=rs.getString("jsh")&&!"".equals(rs.getString("jsh"))){
	        		   info.setSettlementNumber(rs.getString("jsh"));
	        	   }
	        	   
	        	   if(null!=rs.getString("lcysxmNum")&&!"".equals(rs.getString("lcysxmNum"))){
	        		   FundFlowItemCollection fundFlowItemCollection = iFundFlowItem.getFundFlowItemCollection("where number='"+rs.getString("lcysxmNum")+"'");
	        		   if(fundFlowItemCollection.size()==0){
	        			   throw new RuntimeException("找不到流出预算项目编码对应的资金流量项目数据");
	        		   }
	        		   info.setFundFlowItem(fundFlowItemCollection.get(0));
	        	   }
	        	   
	        	   if(null!=rs.getString("cbzxNum")&&!"".equals(rs.getString("cbzxNum"))){
	        		   EntityViewInfo view=new EntityViewInfo();
	        		   FilterInfo filter=new FilterInfo();
	        		   filter.getFilterItems().add(new FilterItemInfo("number",rs.getString("jsfsbm")));
	        		   view.setFilter(filter);
	        		   CostCenterOrgUnitCollection costCenterOrgUnitCollection = iCostCenterOrgUnit.getCostCenterOrgUnitCollection(view);
	        		   if(costCenterOrgUnitCollection.size()==0){
	        			   throw new RuntimeException("找不到成本中心编码对应的成本中心数据");
	        		   }
	        		   info.setCostCenter(costCenterOrgUnitCollection.get(0));
	        	   }
	        	   
	        	   if(null!=rs.getString("zyNum")&&!"".equals(rs.getString("zyNum"))){
	        		   VoucherAbstractCollection voucherAbstractCollection = iVoucherAbstract.getVoucherAbstractCollection("where number='"+rs.getString("zyNum")+"'");
	        		   if(voucherAbstractCollection.size()==0){
	        			   throw new RuntimeException("找不到摘要编码对应的摘要数据");
	        		   }
	        		   info.setDescription(voucherAbstractCollection.get(0).getName());
	        	   }
	        	   
	        	   if(null!=rs.getString("bmNum")&&!"".equals(rs.getString("bmNum"))){
	        		   AdminOrgUnitCollection adminOrgUnitCollection = iAdminOrgUnit.getAdminOrgUnitCollection("where number='"+rs.getString("bmNum")+"'");
	        		   if(adminOrgUnitCollection.size()==0){
	        			   throw new RuntimeException("找不到部门编码对应的部门数据");
	        		   }
	        		   info.setAdminOrgUnit(adminOrgUnitCollection.get(0));
	        	   }
	        	   
	        	   if(null!=rs.getString("ryNum")&&!"".equals(rs.getString("ryNum"))){
	        		   PersonCollection personCollection = iPerson.getPersonCollection("where number='"+rs.getString("ryNum")+"'");
	        		   if(personCollection.size()==0){
	        			   throw new RuntimeException("找不到人员编码对应的人员数据");
	        		   }
	        		   info.setPerson(personCollection.get(0));
	        	   }
	        	   
	        	   if(null==rs.getString("skrlx")||"".equals(rs.getString("skrlx"))){
	        		   throw new RuntimeException("收款人类型不能为空");
	        	   }
	        	   
	        	   if(null==rs.getString("skrmcNum")||"".equals(rs.getString("skrmcNum"))){
	    			   throw new RuntimeException("收款人类型编码不能为空");
	    		   }
	        	   
	        	   if(null==rs.getString("skzh")||"".equals(rs.getString("skzh"))){
	    			   throw new RuntimeException("收款账号不能为空");
	    		   }
	    		   
	    		   if(rs.getString("skrlx").equals("客户")){
	    			   EntityViewInfo view=new EntityViewInfo();
	        		   FilterInfo filter=new FilterInfo();
	        		   filter.getFilterItems().add(new FilterItemInfo("assistantType","1"));
	        		   view.setFilter(filter);
	    			   AsstActTypeCollection asstActTypeCollection = iAsstActType.getAsstActTypeCollection(view);
	    			   if(asstActTypeCollection.size()>0){
	    				   info.setPayeeID(asstActTypeCollection.get(0).getId().toString());
	    				   
	    			   }
	    			   
	    			   view=new EntityViewInfo();
	        		   filter=new FilterInfo();
	        		   filter.getFilterItems().add(new FilterItemInfo("customer.number",rs.getString("skrmcNum")));
	        		   view.setFilter(filter);
	        		   SelectorItemCollection sic=new SelectorItemCollection();
	        		   sic.add(new SelectorItemInfo("*"));
	        		   sic.add(new SelectorItemInfo("customer.name"));
	        		   sic.add(new SelectorItemInfo("customer.number"));
	        		   view.setSelector(sic);
	    			   CustomerCompanyInfoCollection customerCompanyInfoCollection = iCustomerCompanyInfo.getCustomerCompanyInfoCollection(view);
	    			   if(customerCompanyInfoCollection.size()==0){
	    				   throw new RuntimeException("找不到收款人类型编码对应的客户数据");
	    			   }
	    			   info.setPayeeNumber(customerCompanyInfoCollection.get(0).getCustomer().getNumber());
	    			   info.setPayeeName(customerCompanyInfoCollection.get(0).getCustomer().getName());
	    			   
	    			   
	    			   CustomerCompanyBankCollection customerCompanyBankCollection = iCustomerCompanyBank.
	    			   getCustomerCompanyBankCollection("where bankAccount='"+rs.getString("skzh")+"' and customerCompanyInfo.id='"+customerCompanyInfoCollection.get(0).getId().toString()+"'" );
	    			   if(customerCompanyBankCollection.size()==0){
	    				   throw new RuntimeException("找不到收款账号对应的客户银行账号");
	    			   }
	    			   info.setPayeeAccountBank(customerCompanyBankCollection.get(0).getBankAccount());
	            	   info.setPayeeBank(customerCompanyBankCollection.get(0).getBank());
	    		   }else if(rs.getString("skrlx").equals("供应商")){
	    			   EntityViewInfo view=new EntityViewInfo();
	        		   FilterInfo filter=new FilterInfo();
	        		   filter.getFilterItems().add(new FilterItemInfo("assistantType","2"));
	        		   view.setFilter(filter);
	    			   AsstActTypeCollection asstActTypeCollection = iAsstActType.getAsstActTypeCollection(view);
	    			   if(asstActTypeCollection.size()>0){
	    				   info.setPayeeID(asstActTypeCollection.get(0).getId().toString());
	    			   }
	    			   
	    			   view=new EntityViewInfo();
	        		   filter=new FilterInfo();
	        		   filter.getFilterItems().add(new FilterItemInfo("supplier.number",rs.getString("skrmcNum")));
	        		   view.setFilter(filter);
	        		   SelectorItemCollection sic=new SelectorItemCollection();
	        		   sic.add(new SelectorItemInfo("*"));
	        		   sic.add(new SelectorItemInfo("supplier.name"));
	        		   sic.add(new SelectorItemInfo("supplier.number"));
	        		   view.setSelector(sic);
	        		   SupplierCompanyInfoCollection supplierCompanyInfoCollection = iSupplierCompanyInfo.getSupplierCompanyInfoCollection(view);
	    			   if(supplierCompanyInfoCollection.size()==0){
	    				   throw new RuntimeException("找不到收款人类型编码对应的供应商数据");
	    			   }
	    			   info.setPayeeNumber(supplierCompanyInfoCollection.get(0).getSupplier().getNumber());
	    			   info.setPayeeName(supplierCompanyInfoCollection.get(0).getSupplier().getName());
	    			   
	    			   SupplierCompanyBankCollection supplierCompanyBankCollection = iSupplierCompanyBank.
	    			   getSupplierCompanyBankCollection("where bankAccount='"+rs.getString("skzh")+"' and supplierCompanyInfo.id='"+supplierCompanyInfoCollection.get(0).getId().toString()+"'" );
	    			   if(supplierCompanyBankCollection.size()==0){
	    				   throw new RuntimeException("找不到收款账号对应的供应商银行账号");
	    			   }
	    			   info.setPayeeAccountBank(supplierCompanyBankCollection.get(0).getBankAccount());
	            	   info.setPayeeBank(supplierCompanyBankCollection.get(0).getBank());
	    		   }else{
	    			   info.setPayeeNumber(rs.getString("skrmcNum"));
	    			   info.setPayeeName(rs.getString("skrmcNum"));
	    			   
	    			   EntityViewInfo view=new EntityViewInfo();
	        		   FilterInfo filter=new FilterInfo();
	        		   filter.getFilterItems().add(new FilterItemInfo("bankAccountNumber",rs.getString("skzh")));
	        		   view.setFilter(filter);
	        		   SelectorItemCollection sic=new SelectorItemCollection();
	        		   sic.add(new SelectorItemInfo("*"));
	        		   sic.add(new SelectorItemInfo("bank.name"));
	        		   view.setSelector(sic);
	        		   AccountBankCollection accountBankCollection = iAccountBank.getAccountBankCollection(view);
	        		   if(accountBankCollection.size()==0){
	        			   throw new RuntimeException("找不到收款账号对应的银行账号数据");
	        		   }
	            	   info.setPayeeAccountBank(accountBankCollection.get(0).getBankAccountNumber());
	            	   info.setPayeeBank(accountBankCollection.get(0).getBank().getName());
	            	   info.setPayeeAccountBankO(accountBankCollection.get(0));
	            	  
	    		   }
	    		   
	    		   if(null!=rs.getString("skkmNum")&&!"".equals(rs.getString("skkmNum"))){
	        		   AccountViewCollection accountViewCollection = iAccountView.getAccountViewCollection("where number='"+rs.getString("skkmNum")+"'");
	        		   if(accountViewCollection.size()==0){
	        			   throw new RuntimeException("找不到对方（收款）科目编码对应的会计科目数据");
	        		   }
	        		   info.setOppAccount(accountViewCollection.get(0));
	        	   }
	    		   
	    		   if(null!=rs.getString("xmNum")&&!"".equals(rs.getString("xmNum"))){
	    			   EntityViewInfo view=new EntityViewInfo();
	        		   FilterInfo filter=new FilterInfo();
	        		   filter.getFilterItems().add(new FilterItemInfo("number",rs.getString("xmNum")));
	        		   view.setFilter(filter);
	    			   ProjectCollection projectCollection = iProject.getProjectCollection(view);
	    			   if(projectCollection.size()==0){
	        			   throw new RuntimeException("找不到项目编码对应的项目数据");
	        		   }
	        		   info.setProject(projectCollection.get(0));
	    		   }
	    		   
	    		   if(null==rs.getString("billNum")||"".equals(rs.getString("billNum"))){
	    			   throw new RuntimeException("唯一标识编码不能为空");
	    		   }
	    		   info.setSourceBillId(rs.getString("billNum"));
	    		   
	    		   if(null!=rs.getString("skfgjNum")&&!"".equals(rs.getString("skfgjNum"))){
	    			   CountryCollection countryCollection = iCountry.getCountryCollection("where number='"+rs.getString("skfgjNum")+"'");
	    			   if(countryCollection.size()==0){
	    				   throw new RuntimeException("找不到收款方国家编码对应的国家数据");
	    			   }
	    			   info.setFRecCountry(countryCollection.get(0));
	    		   }
	    		   
	    		   if(null!=rs.getString("skfsNum")&&!"".equals(rs.getString("skfsNum"))){
	    			   EntityViewInfo view=new EntityViewInfo();
	        		   FilterInfo filter=new FilterInfo();
	        		   filter.getFilterItems().add(new FilterItemInfo("number",rs.getString("skfsNum")));
	        		   view.setFilter(filter);
	    			   ProvinceCollection provinceCollection = iProvince.getProvinceCollection(view);
	    			   if(provinceCollection.size()==0){
	    				   throw new RuntimeException("找不到收款方省编码对应的省数据");
	    			   }
	    			   info.setRecProvince(provinceCollection.get(0).getName());
	    		   }
	    		   
	    		   if(null!=rs.getString("skfsxNum")&&!"".equals(rs.getString("skfsxNum"))){
	    			   EntityViewInfo view=new EntityViewInfo();
	        		   FilterInfo filter=new FilterInfo();
	        		   filter.getFilterItems().add(new FilterItemInfo("number",rs.getString("skfsxNum")));
	        		   view.setFilter(filter);
	        		   CityCollection cityCollection = iCity.getCityCollection(view);
	    			   if(cityCollection.size()==0){
	    				   throw new RuntimeException("找不到收款方市县编码对应的城市数据");
	    			   }
	    			   info.setRecCity(cityCollection.get(0).getName());
	    		   }
	    		   
	    		   if(null!=rs.getString("ytNum")&&!"".equals(rs.getString("ytNum"))){
	    			   VoucherAbstractCollection voucherAbstractCollection = iVoucherAbstract.getVoucherAbstractCollection("where number='"+rs.getString("ytNum")+"'");
	        		   if(voucherAbstractCollection.size()==0){
	        			   throw new RuntimeException("找不到摘要编码对应的摘要数据");
	        		   }
	        		   info.setUsage(voucherAbstractCollection.get(0).getName());
	    		   }
	    		   
	    		   if(null!=rs.getDate("qwjyrq")){
	    			   info.setExpectDealTime(rs.getDate("qwjyrq"));
	    		   }
	    		   
	    		   if(null!=rs.getString("skrName")&&!"".equals(rs.getString("skrName"))){
	    			   info.setBankAcctName(rs.getString("skrName"));
	    		   }
	        		
	    		   if(null!=rs.getString("skhNum")&&!"".equals(rs.getString("skhNum"))){
	    			   info.setBankNumber(rs.getString("skrName"));
	    		   }
	    		   
	    		   if(null!=rs.getString("skfEmail")&&!"".equals(rs.getString("skfEmail"))){
	    			   info.setEmail(rs.getString("skfEmail"));
	    		   }
	    		   
	    		   //分录科目信息
	    		   PreparedStatement preStateEntry = connect.prepareStatement("select  * from T_EAS_PaymentBillDetail where billParentNum="+rs.getString("billNum"));
	               ResultSet rsEntry = preStateEntry.executeQuery();
	               while (rsEntry.next()) {
	            	   PaymentBillEntryInfo entryInfo=new PaymentBillEntryInfo();
	            	   
	            	   if(null==rsEntry.getBigDecimal("fkje")){
		    			   throw new RuntimeException("付款金额不能为空");
		    		   }
	            	   entryInfo.setActualAmt(rsEntry.getBigDecimal("fkje"));
	            	   
	            	   if(null!=rsEntry.getString("cbzxNum")&&!"".equals(rsEntry.getString("cbzxNum"))){
	            		   EntityViewInfo view=new EntityViewInfo();
		        		   FilterInfo filter=new FilterInfo();
		        		   filter.getFilterItems().add(new FilterItemInfo("number",rs.getString("cbzxNum")));
		        		   view.setFilter(filter);
	            		   CostCenterOrgUnitCollection costCenterOrgUnitCollection = iCostCenterOrgUnit.getCostCenterOrgUnitCollection(view);
	            		   if(costCenterOrgUnitCollection.size()==0){
	            			   throw new RuntimeException("找不到成本中心编码对应的成本中心数据");
	            		   }
	            		   entryInfo.setCostCenter(costCenterOrgUnitCollection.get(0));
		    		   }
	            	   
	            	   if(null!=rsEntry.getString("fylxNum")&&!"".equals(rsEntry.getString("fylxNum"))){
	            		   ExpenseTypeCollection expenseTypeCollection = iExpenseType.getExpenseTypeCollection("where number='"+rs.getString("fylxNum")+"'");
	            		   if(expenseTypeCollection.size()==0){
	            			   throw new RuntimeException("找不到费用类型编码对应的费用类型数据");
	            		   }
	            		   entryInfo.setExpenseType(expenseTypeCollection.get(0));
	            	   }
	            	   
	            	   if(null==rsEntry.getBigDecimal("dfkmbm")){
		    			   throw new RuntimeException("对方科目编码不能为空");
		    		   }
	            	   
	            	   AccountViewCollection accountViewCollection = iAccountView.getAccountViewCollection("where number='"+rsEntry.getBigDecimal("dfkmbm")+"'");
	            	   if(accountViewCollection.size()==0){
	            		   throw new RuntimeException("找不到对方科目编码对应的会计科目数据");
	            	   }
	            	   entryInfo.setOppAccount(accountViewCollection.get(0));
	            	   
	            	   if(null!=rsEntry.getString("zjllxmNum")&&!"".equals(rsEntry.getString("zjllxmNum"))){
		        		   FundFlowItemCollection fundFlowItemCollection = iFundFlowItem.getFundFlowItemCollection("where number='"+rs.getString("zjllxmNum")+"'");
		        		   if(fundFlowItemCollection.size()==0){
		        			   throw new RuntimeException("找不到资金流量项目编码对应的资金流量项目数据");
		        		   }
		        		   entryInfo.setFundFlowItem(fundFlowItemCollection.get(0));
		        	   }
	            	   
	            	   if(null!=rsEntry.getString("bz")&&!"".equals(rsEntry.getString("bz"))){
	            		   entryInfo.setRemark(rsEntry.getString("bz"));
	            	   }
	            	   
	            	   if(null!=rsEntry.getString("flhh")&&!"".equals(rsEntry.getString("flhh"))){
	            		   entryInfo.setSeq(rsEntry.getInt("flhh"));
	            	   }
	            	   
	            	   
	            	   //分录科目核算项目信息
		    		   PreparedStatement preStateAssItems = connect.prepareStatement("select  * from T_CAS_AssItemsForCashPay where billParentNum="+rsEntry.getString("billNum"));
		               ResultSet rsAssItems = preStateAssItems.executeQuery();
		               while (rsAssItems.next()) {
		            	   AssItemsForCashPayInfo assItemsInfo=new AssItemsForCashPayInfo();
		            	   
		            	   if(null==rsAssItems.getBigDecimal("hsxmlb")){
			    			   throw new RuntimeException("核算项目类型编码不能为空");
			    		   }
		            	   
		            	   EntityViewInfo view=new EntityViewInfo();
		        		   FilterInfo filter=new FilterInfo();
		        		   filter.getFilterItems().add(new FilterItemInfo("number",rs.getString("hsxmlb")));
		        		   view.setFilter(filter);
		            	   AsstActTypeCollection asstActTypeCollection = iAsstActType.getAsstActTypeCollection(view);
		            	   if(asstActTypeCollection.size()==0){
		            		   throw new RuntimeException("找不到核算项目类型编码对应的核算项目数据");
		            	   }
		            	   AsstActTypeInfo asstActTypeInfo = asstActTypeCollection.get(0);
		            	   assItemsInfo.setAsstActType(asstActTypeInfo);
		            	   assItemsInfo.setMappingFileds(asstActTypeInfo.getMappingFieldName());
		            	   assItemsInfo.setTableName(asstActTypeInfo.getRealtionDataObject());
		            	   
		            	   if(null==rsAssItems.getBigDecimal("hsxmbm")){
			    			   throw new RuntimeException("核算项目编码不能为空");
			    		   }
		            	   assItemsInfo.setIsSelected(true);
		            	   String sql="select * from "+asstActTypeInfo.getRealtionDataObject()+" where fnumber="+rsAssItems.getBigDecimal("hsxmbm");
		            	   IRowSet iRowSet = SQLExecutorFactory.getRemoteInstance(sql).executeSQL();
		            	   int num=0;
		            	   if(iRowSet.next()){
		            		   num++;
		            		   assItemsInfo.setFromID(iRowSet.getString("fid"));
		            		   assItemsInfo.setFromNumber(iRowSet.getString("fnumber"));
		            	   }
		            	   
		            	   if(num==0){
		            		   throw new RuntimeException("找不到核算项目编码对应的"+asstActTypeInfo.getName()+"数据");
		            	   }
		            	   
		            	   entryInfo.getAssItemsEntries().add(assItemsInfo);
		               }
		               info.getEntries().add(entryInfo);
	               }
	               
		   		   info.setCreator(userInfo);
		   		   info.setLastUpdateUser(userInfo);
		   		   info.setCU(ctrlUnitInfo);
	               IObjectPK iObjectPK = iPaymentBill.addnew(info);
	               
        	   }catch (Exception e) {
        		   if(e.getMessage().length()>200){
        			   msg=e.getMessage().substring(0, 200);
        		   }else{
        			   msg=e.getMessage();
        		   }
        	   }finally{
        		   PreparedStatement ps = connect.prepareStatement("update T_EAS_PaymentBill set clsj=sysdate ,clzt=?,clbz=? where billNum=?");
        		   ps.setString(1, "s");
        		   ps.setString(2, msg);
        		   ps.setString(3, rs.getString("billNum"));
        		   ps.executeUpdate();
        	   }
           }
   
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //第六步：关闭资源
                try {
                    if (resultSet!=null) resultSet.close();
                    if (statement!=null) statement.close();
                    if (connect!=null) connect.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
        }
    }
    
    @Override
    protected void _syncPaymentBill(Context ctx) throws BOSException {
    	try {				 
            siteCustomer();
 	        PreparedStatement preState = connect.prepareStatement("select  * from T_CAS_ReceivingBill where tbzt='s' and clzt<>'s'");
            ResultSet rs = preState.executeQuery();
            
            ICompanyOrgUnit iCompanyOrgUnit = CompanyOrgUnitFactory.getLocalInstance(ctx);
            IFeeType iFeeType = FeeTypeFactory.getLocalInstance(ctx);
            IReceivingBillType iReceivingBillType = ReceivingBillTypeFactory.getLocalInstance(ctx);
            ICurrency iCurrency = CurrencyFactory.getLocalInstance(ctx);
            IAccountBank iAccountBank = AccountBankFactory.getLocalInstance(ctx);
            IAccountView iAccountView = AccountViewFactory.getLocalInstance(ctx);
            ISettBizType iSettBizType = SettBizTypeFactory.getLocalInstance(ctx);
            ISettlementType iSettlementType = SettlementTypeFactory.getLocalInstance(ctx);
            IFundFlowItem iFundFlowItem = FundFlowItemFactory.getLocalInstance(ctx);
            ICostCenterOrgUnit iCostCenterOrgUnit = CostCenterOrgUnitFactory.getLocalInstance(ctx);
            IVoucherAbstract iVoucherAbstract = VoucherAbstractFactory.getLocalInstance(ctx);
            IAdminOrgUnit iAdminOrgUnit = AdminOrgUnitFactory.getLocalInstance(ctx);
            IPerson iPerson = PersonFactory.getLocalInstance(ctx);
            IAsstActType iAsstActType = AsstActTypeFactory.getLocalInstance(ctx);
            ICustomerCompanyInfo iCustomerCompanyInfo = CustomerCompanyInfoFactory.getLocalInstance(ctx);
            ICustomerCompanyBank iCustomerCompanyBank = CustomerCompanyBankFactory.getLocalInstance(ctx);
            ISupplierCompanyBank iSupplierCompanyBank = SupplierCompanyBankFactory.getLocalInstance(ctx);
            ISupplierCompanyInfo iSupplierCompanyInfo = SupplierCompanyInfoFactory.getLocalInstance(ctx);
            IProject iProject = ProjectFactory.getLocalInstance(ctx);
            ICountry iCountry = CountryFactory.getLocalInstance(ctx);
            IProvince iProvince = ProvinceFactory.getLocalInstance(ctx);
            ICity iCity = CityFactory.getLocalInstance(ctx);
            IExpenseType iExpenseType = ExpenseTypeFactory.getLocalInstance(ctx);
            IReceivingBill iReceivingBill = ReceivingBillFactory.getLocalInstance(ctx);
            
            while(rs.next()){
         	   String msg="";
         	   try{
         		   ReceivingBillInfo info=new ReceivingBillInfo();
 	        	   CompanyOrgUnitCollection gsNumCollection = iCompanyOrgUnit.getCompanyOrgUnitCollection("where number='"+rs.getString("gsNum")+"'");
 	        	   if(gsNumCollection.size()==0){
 	        		   throw new RuntimeException("找不到公司编码对应公司数据");
 	        	   }
 	        	   info.setCompany(gsNumCollection.get(0));
 	        	   
 	        	   if(null!=rs.getString("sjsgNum")&&!"".equals(rs.getString("sjsgNum"))){
 	        		   CompanyOrgUnitCollection dlgsNumCollection = iCompanyOrgUnit.getCompanyOrgUnitCollection("where number='"+rs.getString("sjsgNum")+"'");
 	            	   if(dlgsNumCollection.size()==0){
 	            		   throw new RuntimeException("找不到代理实际收款公司编码对应的公司数据");
 	            	   }
 	            	   info.setRealRecCompany(dlgsNumCollection.get(0));
 	        	   }
 	        	   
 	        	   if(null==rs.getDate("ywrq")||"".equals(rs.getString("ywrq"))){
 	        		   throw new RuntimeException("业务日期不能为空");
 	        	   }
 	        	   info.setBizDate(rs.getDate("ywrq"));
 	        	   
 	        	   if(null!=rs.getString("sflbNum")&&!"".equals(rs.getString("sflbNum"))){
 	        		   FeeTypeCollection feeTypeCollection = iFeeType.getFeeTypeCollection("where number='"+rs.getString("sflbNum")+"'");
 	        		   if(feeTypeCollection.size()==0){
 	        			   throw new RuntimeException("找不到收付类别编码对应的收付类别数据");
 	        		   }
 	        		   info.setFeeType(feeTypeCollection.get(0));
 	        	   }
 	        	   
 	        	   if(null==rs.getString("sklxNum")||"".equals(rs.getString("sklxNum"))){
 	        		   throw new RuntimeException("收款类型编码不能为空");
 	        	   }
 	        	   
 	        	   ReceivingBillTypeCollection receivingBillTypeCollection = iReceivingBillType.getReceivingBillTypeCollection("where number='"+rs.getString("sklxNum")+"'");
 	    		   if(receivingBillTypeCollection.size()==0){
 	    			   throw new RuntimeException("找不到收款类型编码对应的收款类型数据");
 	    		   }
 	    		   info.setRecBillType(receivingBillTypeCollection.get(0));
 	        	   
 	        	   
 	        	   if(null==rs.getString("fkbb")||"".equals(rs.getString("fkbb"))){
 	        		   throw new RuntimeException("付款币别编码不能为空");
 	        	   }
 	        		   
 	        	   CurrencyCollection currencyCollection = iCurrency.getCurrencyCollection("where number='"+rs.getString("fkbb")+"'");
 	    		   if(currencyCollection.size()==0){
 	    			   throw new RuntimeException("找不到付款币别编码对应的币别数据");
 	    		   }
 	    		   info.setCurrency(currencyCollection.get(0));
 	        	   
 	        	   
 	        	   if(null==rs.getBigDecimal("hl")||"".equals(rs.getString("hl"))){
 	        		   throw new RuntimeException("汇率不能为空");
 	        	   }
 	        	   info.setExchangeRate(rs.getBigDecimal("hl"));
 	        	   
 	        	   if(null!=rs.getString("skzhNum")&&!"".equals(rs.getString("skzhNum"))){
 	        		   AccountBankCollection accountBankCollection = iAccountBank.getAccountBankCollection("where number='"+rs.getString("skzhNum")+"'");
 	        		   if(accountBankCollection.size()==0){
 	        			   throw new RuntimeException("找不到收款账户编码对应的银行账号数据");
 	        		   }
 	        		   info.setPayeeAccountBank(accountBankCollection.get(0));
 	        	   }
 	        	   
 	        	   if(null!=rs.getString("skkmNum")&&!"".equals(rs.getString("skkmNum"))){
 	        		   AccountViewCollection accountViewCollection = iAccountView.getAccountViewCollection("where number='"+rs.getString("skkmNum")+"'");
 	        		   if(accountViewCollection.size()==0){
 	        			   throw new RuntimeException("找不到收款科目编码对应的会计科目数据");
 	        		   }
 	        		   info.setPayeeAccount(accountViewCollection.get(0));
 	        	   }
 	        	   
 	        	   if(null!=rs.getString("ywzlNum")&&!"".equals(rs.getString("ywzlNum"))){
 	        		   SettBizTypeCollection settBizTypeCollection = iSettBizType.getSettBizTypeCollection("where number='"+rs.getString("ywzlNum")+"'");
 	        		   if(settBizTypeCollection.size()==0){
 	        			   throw new RuntimeException("找不到业务种类编码对应的业务种类数据");
 	        		   }
 	        		   info.setBizType(settBizTypeCollection.get(0));
 	        	   }
 	        	   
 	        	   if(null!=rs.getString("jsfsbm")&&!"".equals(rs.getString("jsfsbm"))){
 	        		   EntityViewInfo view=new EntityViewInfo();
 	        		   FilterInfo filter=new FilterInfo();
 	        		   filter.getFilterItems().add(new FilterItemInfo("number",rs.getString("jsfsbm")));
 	        		   view.setFilter(filter);
 	        		   SettlementTypeCollection settlementTypeCollection = iSettlementType.getSettlementTypeCollection(view);
 	        		   if(settlementTypeCollection.size()==0){
 	        			   throw new RuntimeException("找不到结算方式编码对应的结算方式数据");
 	        		   }
 	        		   info.setSettlementType(settlementTypeCollection.get(0));
 	        	   }
 	        	   
 	        	   if(null!=rs.getString("jsh")&&!"".equals(rs.getString("jsh"))){
 	        		   info.setSettlementNumber(rs.getString("jsh"));
 	        	   }
 	        	   
 	        	   if(null!=rs.getString("lcysxmNum")&&!"".equals(rs.getString("lcysxmNum"))){
 	        		   FundFlowItemCollection fundFlowItemCollection = iFundFlowItem.getFundFlowItemCollection("where number='"+rs.getString("lcysxmNum")+"'");
 	        		   if(fundFlowItemCollection.size()==0){
 	        			   throw new RuntimeException("找不到流出预算项目编码对应的资金流量项目数据");
 	        		   }
 	        		   info.setFundFlowItem(fundFlowItemCollection.get(0));
 	        	   }
 	        	   
 	        	   if(null!=rs.getString("cbzxNum")&&!"".equals(rs.getString("cbzxNum"))){
 	        		   EntityViewInfo view=new EntityViewInfo();
 	        		   FilterInfo filter=new FilterInfo();
 	        		   filter.getFilterItems().add(new FilterItemInfo("number",rs.getString("jsfsbm")));
 	        		   view.setFilter(filter);
 	        		   CostCenterOrgUnitCollection costCenterOrgUnitCollection = iCostCenterOrgUnit.getCostCenterOrgUnitCollection(view);
 	        		   if(costCenterOrgUnitCollection.size()==0){
 	        			   throw new RuntimeException("找不到成本中心编码对应的成本中心数据");
 	        		   }
 	        		   info.setCostCenter(costCenterOrgUnitCollection.get(0));
 	        	   }
 	        	   
 	        	   if(null!=rs.getString("zyNum")&&!"".equals(rs.getString("zyNum"))){
 	        		   VoucherAbstractCollection voucherAbstractCollection = iVoucherAbstract.getVoucherAbstractCollection("where number='"+rs.getString("zyNum")+"'");
 	        		   if(voucherAbstractCollection.size()==0){
 	        			   throw new RuntimeException("找不到摘要编码对应的摘要数据");
 	        		   }
 	        		   info.setDescription(voucherAbstractCollection.get(0).getName());
 	        	   }
 	        	   
 	        	   if(null!=rs.getString("bmNum")&&!"".equals(rs.getString("bmNum"))){
 	        		   AdminOrgUnitCollection adminOrgUnitCollection = iAdminOrgUnit.getAdminOrgUnitCollection("where number='"+rs.getString("bmNum")+"'");
 	        		   if(adminOrgUnitCollection.size()==0){
 	        			   throw new RuntimeException("找不到部门编码对应的部门数据");
 	        		   }
 	        		   info.setAdminOrgUnit(adminOrgUnitCollection.get(0));
 	        	   }
 	        	   
 	        	   if(null!=rs.getString("ryNum")&&!"".equals(rs.getString("ryNum"))){
 	        		   PersonCollection personCollection = iPerson.getPersonCollection("where number='"+rs.getString("ryNum")+"'");
 	        		   if(personCollection.size()==0){
 	        			   throw new RuntimeException("找不到人员编码对应的人员数据");
 	        		   }
 	        		   info.setPerson(personCollection.get(0));
 	        	   }
 	        	   
 	        	   if(null==rs.getString("fkrlx")||"".equals(rs.getString("fkrlx"))){
 	        		   throw new RuntimeException("付款人类型不能为空");
 	        	   }
 	        	   
 	        	   if(null==rs.getString("fkrmcNum")||"".equals(rs.getString("fkrmcNum"))){
 	    			   throw new RuntimeException("付款人类型编码不能为空");
 	    		   }
 	        	   
 	        	   if(null==rs.getString("fkzh")||"".equals(rs.getString("fkzh"))){
 	    			   throw new RuntimeException("付款账号不能为空");
 	    		   }
 	    		   
 	    		   if(rs.getString("fkrlx").equals("客户")){
 	    			   EntityViewInfo view=new EntityViewInfo();
 	        		   FilterInfo filter=new FilterInfo();
 	        		   filter.getFilterItems().add(new FilterItemInfo("assistantType","1"));
 	        		   view.setFilter(filter);
 	    			   AsstActTypeCollection asstActTypeCollection = iAsstActType.getAsstActTypeCollection(view);
 	    			   if(asstActTypeCollection.size()>0){
 	    				   info.setPayerID(asstActTypeCollection.get(0).getId().toString());
 	    			   }
 	    			   
 	    			   view=new EntityViewInfo();
 	        		   filter=new FilterInfo();
 	        		   filter.getFilterItems().add(new FilterItemInfo("customer.number",rs.getString("fkrmcNum")));
 	        		   view.setFilter(filter);
 	        		   SelectorItemCollection sic=new SelectorItemCollection();
 	        		   sic.add(new SelectorItemInfo("*"));
 	        		   sic.add(new SelectorItemInfo("customer.name"));
 	        		   sic.add(new SelectorItemInfo("customer.number"));
 	        		   view.setSelector(sic);
 	    			   CustomerCompanyInfoCollection customerCompanyInfoCollection = iCustomerCompanyInfo.getCustomerCompanyInfoCollection(view);
 	    			   if(customerCompanyInfoCollection.size()==0){
 	    				   throw new RuntimeException("找不到付款人类型编码对应的客户数据");
 	    			   }
 	    			   info.setPayerNumber(customerCompanyInfoCollection.get(0).getCustomer().getNumber());
 	    			   info.setPayerName(customerCompanyInfoCollection.get(0).getCustomer().getName());
 	    			   
 	    			   
 	    			   CustomerCompanyBankCollection customerCompanyBankCollection = iCustomerCompanyBank
 	    			   .getCustomerCompanyBankCollection("where bankAccount='"+rs.getString("fkzh")+"' and customerCompanyInfo.id='"+customerCompanyInfoCollection.get(0).getId().toString()+"'" );
 	    			   if(customerCompanyBankCollection.size()==0){
 	    				   throw new RuntimeException("找不到付款账号对应的客户银行账号");
 	    			   }
 	    			   info.setPayerAccountBank(customerCompanyBankCollection.get(0).getBankAccount());
 	            	   info.setPayerBank(customerCompanyBankCollection.get(0).getBank());
 	    		   }else if(rs.getString("fkrlx").equals("供应商")){
 	    			   EntityViewInfo view=new EntityViewInfo();
 	        		   FilterInfo filter=new FilterInfo();
 	        		   filter.getFilterItems().add(new FilterItemInfo("assistantType","2"));
 	        		   view.setFilter(filter);
 	    			   AsstActTypeCollection asstActTypeCollection = iAsstActType.getAsstActTypeCollection(view);
 	    			   if(asstActTypeCollection.size()>0){
 	    				   info.setPayerID(asstActTypeCollection.get(0).getId().toString());
 	    			   }
 	    			   
 	    			   view=new EntityViewInfo();
 	        		   filter=new FilterInfo();
 	        		   filter.getFilterItems().add(new FilterItemInfo("supplier.number",rs.getString("fkrmcNum")));
 	        		   view.setFilter(filter);
 	        		   SelectorItemCollection sic=new SelectorItemCollection();
 	        		   sic.add(new SelectorItemInfo("*"));
 	        		   sic.add(new SelectorItemInfo("supplier.name"));
 	        		   sic.add(new SelectorItemInfo("supplier.number"));
 	        		   view.setSelector(sic);
 	        		   SupplierCompanyInfoCollection supplierCompanyInfoCollection = iSupplierCompanyInfo.getSupplierCompanyInfoCollection(view);
 	    			   if(supplierCompanyInfoCollection.size()==0){
 	    				   throw new RuntimeException("找不到付款人类型编码对应的供应商数据");
 	    			   }
 	    			   info.setPayerNumber(supplierCompanyInfoCollection.get(0).getSupplier().getNumber());
 	    			   info.setPayerName(supplierCompanyInfoCollection.get(0).getSupplier().getName());
 	    			   
 	    			   SupplierCompanyBankCollection supplierCompanyBankCollection = iSupplierCompanyBank.
 	    			   getSupplierCompanyBankCollection("where bankAccount='"+rs.getString("fkzh")+"' and supplierCompanyInfo.id='"+supplierCompanyInfoCollection.get(0).getId().toString()+"'" );
 	    			   if(supplierCompanyBankCollection.size()==0){
 	    				   throw new RuntimeException("找不到付款账号对应的供应商银行账号");
 	    			   }
 	    			   info.setPayerAccountBank(supplierCompanyBankCollection.get(0).getBankAccount());
 	            	   info.setPayerBank(supplierCompanyBankCollection.get(0).getBank());
 	    		   }else{
 	    			   info.setPayerNumber(rs.getString("fkrmcNum"));
 	    			   info.setPayerName(rs.getString("fkrmcNum"));
 	    			   
 	    			   EntityViewInfo view=new EntityViewInfo();
 	        		   FilterInfo filter=new FilterInfo();
 	        		   filter.getFilterItems().add(new FilterItemInfo("bankAccountNumber",rs.getString("fkzh")));
 	        		   view.setFilter(filter);
 	        		   SelectorItemCollection sic=new SelectorItemCollection();
 	        		   sic.add(new SelectorItemInfo("*"));
 	        		   sic.add(new SelectorItemInfo("bank.name"));
 	        		   view.setSelector(sic);
 	        		   AccountBankCollection accountBankCollection = iAccountBank.getAccountBankCollection(view);
 	        		   if(accountBankCollection.size()==0){
 	        			   throw new RuntimeException("找不到付款账号对应的银行账号数据");
 	        		   }
 	            	   info.setPayerAccountBank(accountBankCollection.get(0).getBankAccountNumber());
 	            	   info.setPayerBank(accountBankCollection.get(0).getBank().getName());
 	            	   info.setPayerAccountBankO(accountBankCollection.get(0));
 	            	  
 	    		   }
 	    		   
 	    		   if(null!=rs.getString("dfskkmNum")&&!"".equals(rs.getString("dfskkmNum"))){
 	        		   AccountViewCollection accountViewCollection = iAccountView.getAccountViewCollection("where number='"+rs.getString("dfskkmNum")+"'");
 	        		   if(accountViewCollection.size()==0){
 	        			   throw new RuntimeException("找不到对方（收款）科目编码对应的会计科目数据");
 	        		   }
 	        		   info.setOppAccount(accountViewCollection.get(0));
 	        	   }
 	    		   
 	    		   if(null!=rs.getString("xmNum")&&!"".equals(rs.getString("xmNum"))){
 	    			   EntityViewInfo view=new EntityViewInfo();
 	        		   FilterInfo filter=new FilterInfo();
 	        		   filter.getFilterItems().add(new FilterItemInfo("number",rs.getString("xmNum")));
 	        		   view.setFilter(filter);
 	    			   ProjectCollection projectCollection = iProject.getProjectCollection(view);
 	    			   if(projectCollection.size()==0){
 	        			   throw new RuntimeException("找不到项目编码对应的项目数据");
 	        		   }
 	        		   info.setProject(projectCollection.get(0));
 	    		   }
 	    		   
 	    		   if(null==rs.getString("billNum")||"".equals(rs.getString("billNum"))){
 	    			   throw new RuntimeException("唯一标识编码不能为空");
 	    		   }
 	    		   info.setSourceBillId(rs.getString("billNum"));
 	    		   
 	    		   if(null!=rs.getString("fkfgjNum")&&!"".equals(rs.getString("fkfgjNum"))){
 	    			  info.setPayerBank(rs.getString("fkfgjNum"));
 	    		   }
 	    		   
 	    		   //分录科目信息
 	    		   PreparedStatement preStateEntry = connect.prepareStatement("select  * from T_CAS_ReceivingBillEntry where billParentNum="+rs.getString("billNum"));
 	               ResultSet rsEntry = preStateEntry.executeQuery();
 	               while (rsEntry.next()) {
 	            	   ReceivingBillEntryInfo entryInfo=new ReceivingBillEntryInfo();
 	            	   
 	            	   if(null==rsEntry.getBigDecimal("skje")){
 		    			   throw new RuntimeException("收款金额不能为空");
 		    		   }
 	            	   entryInfo.setActualAmt(rsEntry.getBigDecimal("skje"));
 	            	   
 	            	   if(null!=rsEntry.getString("cbzxNum")&&!"".equals(rsEntry.getString("cbzxNum"))){
 	            		   EntityViewInfo view=new EntityViewInfo();
 		        		   FilterInfo filter=new FilterInfo();
 		        		   filter.getFilterItems().add(new FilterItemInfo("number",rs.getString("cbzxNum")));
 		        		   view.setFilter(filter);
 	            		   CostCenterOrgUnitCollection costCenterOrgUnitCollection = iCostCenterOrgUnit.getCostCenterOrgUnitCollection(view);
 	            		   if(costCenterOrgUnitCollection.size()==0){
 	            			   throw new RuntimeException("找不到成本中心编码对应的成本中心数据");
 	            		   }
 	            		   entryInfo.setCostCenter(costCenterOrgUnitCollection.get(0));
 		    		   }
 	            	   
 	            	   if(null!=rsEntry.getString("fylxNum")&&!"".equals(rsEntry.getString("fylxNum"))){
 	            		   ExpenseTypeCollection expenseTypeCollection = iExpenseType.getExpenseTypeCollection("where number='"+rs.getString("fylxNum")+"'");
 	            		   if(expenseTypeCollection.size()==0){
 	            			   throw new RuntimeException("找不到费用类型编码对应的费用类型数据");
 	            		   }
 	            		   entryInfo.setExpenseType(expenseTypeCollection.get(0));
 	            	   }
 	            	   
 	            	   if(null==rsEntry.getBigDecimal("dfkmbm")){
 		    			   throw new RuntimeException("对方科目编码不能为空");
 		    		   }
 	            	   
 	            	   AccountViewCollection accountViewCollection = iAccountView.getAccountViewCollection("where number='"+rsEntry.getBigDecimal("dfkmbm")+"'");
 	            	   if(accountViewCollection.size()==0){
 	            		   throw new RuntimeException("找不到对方科目编码对应的会计科目数据");
 	            	   }
 	            	   entryInfo.setOppAccount(accountViewCollection.get(0));
 	            	   
 	            	   if(null!=rsEntry.getString("zjllxmNum")&&!"".equals(rsEntry.getString("zjllxmNum"))){
 		        		   FundFlowItemCollection fundFlowItemCollection = iFundFlowItem.getFundFlowItemCollection("where number='"+rs.getString("zjllxmNum")+"'");
 		        		   if(fundFlowItemCollection.size()==0){
 		        			   throw new RuntimeException("找不到资金流量项目编码对应的资金流量项目数据");
 		        		   }
 		        		   entryInfo.setFundFlowItem(fundFlowItemCollection.get(0));
 		        	   }
 	            	   
 	            	   if(null!=rsEntry.getString("bz")&&!"".equals(rsEntry.getString("bz"))){
 	            		   entryInfo.setRemark(rsEntry.getString("bz"));
 	            	   }
 	            	   
 	            	   if(null!=rsEntry.getString("flhh")&&!"".equals(rsEntry.getString("flhh"))){
 	            		   entryInfo.setSeq(rsEntry.getInt("flhh"));
 	            	   }
 	            	   
 	            	   
 	            	   //分录科目核算项目信息
 		    		   PreparedStatement preStateAssItems = connect.prepareStatement("select  * from T_CAS_AssItemsForCashRec where billParentNum="+rsEntry.getString("billNum"));
 		               ResultSet rsAssItems = preStateAssItems.executeQuery();
 		               while (rsAssItems.next()) {
 		            	   AssItemsForCashRecInfo assItemsInfo=new AssItemsForCashRecInfo();
 		            	   
 		            	   if(null==rsAssItems.getBigDecimal("hsxmlb")){
 			    			   throw new RuntimeException("核算项目类型编码不能为空");
 			    		   }
 		            	   
 		            	   EntityViewInfo view=new EntityViewInfo();
 		        		   FilterInfo filter=new FilterInfo();
 		        		   filter.getFilterItems().add(new FilterItemInfo("number",rs.getString("hsxmlb")));
 		        		   view.setFilter(filter);
 		            	   AsstActTypeCollection asstActTypeCollection = iAsstActType.getAsstActTypeCollection(view);
 		            	   if(asstActTypeCollection.size()==0){
 		            		   throw new RuntimeException("找不到核算项目类型编码对应的核算项目数据");
 		            	   }
 		            	   AsstActTypeInfo asstActTypeInfo = asstActTypeCollection.get(0);
 		            	   assItemsInfo.setAsstActType(asstActTypeInfo);
 		            	   assItemsInfo.setMappingFileds(asstActTypeInfo.getMappingFieldName());
 		            	   assItemsInfo.setTableName(asstActTypeInfo.getRealtionDataObject());
 		            	   
 		            	   if(null==rsAssItems.getBigDecimal("hsxmbm")){
 			    			   throw new RuntimeException("核算项目编码不能为空");
 			    		   }
 		            	   assItemsInfo.setIsSelected(true);
 		            	   String sql="select * from "+asstActTypeInfo.getRealtionDataObject()+" where fnumber="+rsAssItems.getBigDecimal("hsxmbm");
 		            	   IRowSet iRowSet = SQLExecutorFactory.getRemoteInstance(sql).executeSQL();
 		            	   int num=0;
 		            	   if(iRowSet.next()){
 		            		   num++;
 		            		   assItemsInfo.setFromID(iRowSet.getString("fid"));
 		            		   assItemsInfo.setFromNumber(iRowSet.getString("fnumber"));
 		            	   }
 		            	   
 		            	   if(num==0){
 		            		   throw new RuntimeException("找不到核算项目编码对应的"+asstActTypeInfo.getName()+"数据");
 		            	   }
 		            	   
 		            	   entryInfo.getAssItemsEntries().add(assItemsInfo);
 		               }
 		               info.getEntries().add(entryInfo);
 	               }
		   		   info.setCreator(userInfo);
		   		   info.setLastUpdateUser(userInfo);
		   		   info.setCU(ctrlUnitInfo);
 	               iReceivingBill.addnew(info);
         	   }catch (Exception e) {
         		  if(e.getMessage().length()>200){
	       			   msg=e.getMessage().substring(0, 200);
	       		  }else{
	       			   msg=e.getMessage();
	       		  }
         	   }finally{
         		   PreparedStatement ps = connect.prepareStatement("update T_CAS_ReceivingBill set clsj=sysdate ,clzt=?,clbz=? where billNum=?");
         		   ps.setString(1, "s");
         		   ps.setString(2, msg);
         		   ps.setString(3, rs.getString("billNum"));
         		   ps.executeUpdate();
         	   }
            }
    
         } catch (Exception e) {
             e.printStackTrace();
         }finally {
             //第六步：关闭资源
                 try {
                     if (resultSet!=null) resultSet.close();
                     if (statement!=null) statement.close();
                     if (connect!=null) connect.close();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }
         }
    }
}